<template>
    <div class="app-container">
        <!-- 搜索工作栏 -->
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="链接URL" prop="linkUrl">
                <el-input
                    v-model="queryParams.linkUrl"
                    placeholder="请输入链接URL"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="域名" prop="domainId">
                <el-select
                    v-model="queryParams.domainId"
                    style="width: 100%"
                    filterable
                    clearable
                    placeholder="请选择域名"
                >
                    <el-option
                        v-for="(item, index) in domainList"
                        :key="index"
                        :label="item.domain"
                        :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select
                    v-model="queryParams.status"
                    placeholder="请选择状态"
                    clearable
                    @keyup.enter.native="handleQuery"
                >
                    <el-option :label="'正常'" :value="1"></el-option>
                    <el-option :label="'停用'" :value="0"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    @click="handleQuery"
                    >搜索</el-button
                >
                <el-button icon="el-icon-refresh" @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="openForm({})"
                    >新增</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    plain
                    size="mini"
                    @click="patchDel"
                    type="danger"
                    icon="el-icon-delete"
                    >批量删除</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    :loading="exportLoading"
                    v-hasPermi="['tk:links:export']"
                    >导出</el-button
                >
            </el-col>
        </el-row>
        <el-table
            v-loading="loading"
            :data="list"
            :stripe="true"
            :show-overflow-tooltip="true"
            border
            size="mini"
            max-height="680"
            @selection-change="handleSelectionChange"
        >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
                label="编号"
                prop="id"
                width="120"
                align="center"
            />
            <el-table-column label="链接URL" prop="linkUrl" align="center">
                <template slot-scope="scope">
                    <el-tag
                        size="mini"
                        @click="copyText(scope.row)"
                        class="lk-p"
                        >{{ scope.row.linkUrl }}</el-tag
                    >
                </template>
            </el-table-column>
            <el-table-column label="域名" prop="domain" align="center" />

            <el-table-column
                label="链接描述"
                prop="linkDescription"
                align="center"
            />
            <el-table-column label="问候语" prop="greetings" align="center" />
            <el-table-column
                label="ip限制"
                prop="ipAccessRestriction"
                align="center"
            >
                <template v-slot="scope">
                    <el-tag size="mini" type="info">{{
                        scope.row.ipAccessRestriction === 1 ? "限制" : "否"
                    }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="随机打乱" prop="random" align="center">
                <template v-slot="scope">
                    <el-tag size="mini" type="info">{{
                        scope.row.random === 1 ? "打乱" : "否"
                    }}</el-tag>
                </template>
            </el-table-column>

            <el-table-column
                label="创建日期"
                align="center"
                prop="createTime"
                width="150"
                show-overflow-tooltip
            >
                <template v-slot="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column
                label="操作人"
                prop="nickname"
                align="center"
            ></el-table-column>
            <el-table-column label="状态" prop="status" align="center">
                <template v-slot="scope">
                    <el-switch
                        v-model="scope.row.status"
                        :active-value="1"
                        :inactive-value="0"
                        @change="handleStatusChange(scope.row)"
                    />
                </template>
            </el-table-column>

            <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
            >
                <template v-slot="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="openForm(scope.row)"
                        v-hasPermi="['tk:links:update']"
                        >修改</el-button
                    >

                    <!-- v-hasPermi="['tk:links:delete']" -->
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        v-hasPermi="['tk:links:delete']"
                        >删除</el-button
                    >
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNo"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
        <LinkForm ref="formRef" @success="getList" />
    </div>
</template>
<script>
import {
    getLinksList,
    deleteLinks,
    exportLinks,
    patchDeleteLinks,
    updateLinksStatus,
} from "@/api/links";
import { getDomainList } from "@/api/domain";
import LinkForm from "./linkForm";
import router from "@/router/index";
import { Loading } from "element-ui";
import copy from "copy-to-clipboard";
export default {
    components: {
        LinkForm,
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            exportLoading: false,
            // 表格数据
            list: [],
            domainList: [],
            total: 0,
            // 显示搜索条件
            showSearch: true,
            queryParams: {
                pageNo: 1,
                pageSize: 10,
            },
            ids: [],
        };
    },
    methods: {
        // 复制文本方法
        async copyText(row) {
            const url = "https://" + row.domain + "/?url=" + row.linkUrl;
            try {
                await copy(url);
                this.$message.success("复制成功");
            } catch (err) {
                this.$message.error("复制失败");
            }
        },
        hasSelectdIds() {
            if (this.ids.length === 0) {
                this.$message.info("请选择要操作的项");
            }
            return this.ids.length > 0;
        },
        async getList() {
            this.loading = true;
            const { code, data } = await getLinksList(this.queryParams);
            console.log(data, "0000");
            if (code === 0) {
                this.list = data.list;
                this.total = data.total;
            }

            this.loading = false;
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNo = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            console.log(this.$refs, "this.$refs['queryForm']");
            this.$refs["queryForm"].resetFields();
            this.form;
            this.handleQuery();
        },
        /** 添加/修改操作 */
        openForm(row) {
            this.$refs["formRef"].open(row);
        },
        /** 删除按钮操作 */
        async handleDelete(row) {
            const id = row.id;
            await this.$modal
                .confirm(
                    "请注意，该链接下的所有工单及号码信息将被永久删除，请谨慎操作！"
                )
                .then(async () => {
                    const res = await deleteLinks({
                        id: id,
                    });
                    if (res.code === 0) {
                        await this.getList();
                        this.$modal.msgSuccess("删除成功");
                    } else {
                        this.$modal.msgError(res.msg);
                    }
                })
                .catch(() => {});
        },

        async getDomains() {
            const res = await getDomainList({
                pageNo: 1,
                pageSize: 3000,
            });
            if (res.code === 0) {
                this.domainList = res.data.list;
            }
        },

        /** 导出按钮操作 */
        // async handleExport() {
        //     await this.$modal.confirm("是否确认导出所有工单数据项?");
        //     let downloadLoadingInstance;
        //     try {
        //         this.exportLoading = true;
        //         downloadLoadingInstance = Loading.service({
        //             text: "正在导出数据，请稍候",
        //             spinner: "el-icon-loading",
        //             background: "rgba(0, 0, 0, 0.7)",
        //         });
        //         const res = await exportLinks(this.queryParams);
        //         let routerName = router.app._route.meta.title;
        //         let fileName = "链接-";
        //         if (routerName) {
        //             fileName = routerName + "-";
        //         }
        //         this.$download.excel(
        //             res,
        //             fileName +
        //                 this.parseTime(new Date(), "{y}{m}{d}{h}{i}{s}") +
        //                 ".xls"
        //         );
        //     } finally {
        //         this.exportLoading = false;
        //         downloadLoadingInstance.close();
        //     }
        // },
        handleExport() {
            const queryParams = this.queryParams;
            this.$modal
                .confirm("是否确认导出所有链接数据项?")
                .then(() => {
                    this.exportLoading = true;
                    return exportLinks(queryParams);
                })
                .then((response) => {
                    this.$download.excel(response, "链接数据.xls");
                    this.exportLoading = false;
                })
                .catch(() => {});
        },
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
        },
        async pathDelete() {
            const { code, msg } = await patchDeleteLinks({
                ids: this.ids.join(","),
            });
            if (code === 0) {
                this.$modal.msgSuccess("删除成功");
                this.getList();
            } else {
                this.$modal.msgError(msg);
            }
        },
        patchDel() {
            const hasIds = this.hasSelectdIds();
            if (hasIds) {
                this.$confirm(
                    "请注意，该链接下的所有工单及号码信息将被永久删除，请谨慎操作！",
                    "提示",
                    {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }
                )
                    .then(() => {
                        this.pathDelete();
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除",
                        });
                    });
            }
        },
        async handleStatusChange(row) {
            const res = await updateLinksStatus({
                status: row.status,
                ids: row.id,
            });
            if (res.code === 0) {
                this.$modal.msgSuccess("状态更新成功");
            }
        },
    },
    created() {
        this.getList();
        this.getDomains();
    },
};
</script>
<style scoped>
.lk-p {
    cursor: pointer;
}
</style>
